document.addEventListener('DOMContentLoaded', function () {
    const startCrawlingButton = document.getElementById('startCrawling');
    const statusDiv = document.getElementById('status');

    startCrawlingButton.addEventListener('click', function () {
        statusDiv.textContent = '正在启动爬取...';
        chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
            const timeoutId = setTimeout(() => {
                statusDiv.textContent = '请求超时，请重试。';
            }, 5000); // 设置 5 秒超时时间

            chrome.tabs.sendMessage(tabs[0].id, { action: 'startCrawling' }, function (response) {
                clearTimeout(timeoutId); // 清除超时定时器
                if (chrome.runtime.lastError) {
                    statusDiv.textContent = '出错啦: ' + chrome.runtime.lastError.message;
                } else {
                    statusDiv.textContent = '已发送爬取请求，结果: ' + response.message;
                }
            });
        });
    });
});    